<?php
class FormsController extends ManagerController {
	public function FormsController(){
		parent::ManagerController();
	}
	
	/**
	 * 自定义表单列表
	 */
	public function index(){
		$form_mod = new FormsModel();
		$forms = $form_mod->find(array('site_id'=>SITE_ID));
		
		$this->assign('forms', $forms);
		$this->display();
	}
	
	public function test(){
		if(IS_POST){
			$form_name = self::get_post_val('form_name');
			$h = new FormHelper(array('name'=>$form_name));
			
			$data = $h->get_post_data();
			$html = var_export($data, true);
			self::ajax_error('<pre>'.$html.'</pre>');
		}
		$this->assign('name', self::get_request_val('name'));
		$this->display();
	}
	
	/**
	 * 保存POST数据
	 */
	public function save(){
		$title = self::get_post_val('title');
		$desc = self::get_post_val('desc');
		$name = self::get_post_val('name');
		$owner = self::get_post_val('owner', 'user');
		//验证合法性
		if(empty($title) || utf8_strlen($title)<1 || utf8_strlen($title)>20){
			self::ajax_error('标题长度应该在1至20之间！');
		}
		if (!preg_match('/[[a-z|A-Z][a-z|A-Z|0-9|_]+/i', $name)) {
			self::ajax_error('名称不合法！');
		}
		$model = self::get_post_val('model');
		if(empty($model)){
			self::ajax_error('表单模型不能为空！');
		}
		
		$mutil_lan = false;
		if(isset($_POST['mutil_lan'])){
			$mutil_lan = true;
		}
		$form = array(
			'title' => $title,
			'name' => $name,
			'desc' => $desc,
			'site_id' => SITE_ID,
			'mutil_lan' => $mutil_lan,
			'owner' => $owner,
			'model' => $model,
		);

		if(isset($_POST['list_view_code'])){
			$list_views = $_POST['list_view_code'];
			$i=100;
			$list_fields = array();
			foreach ($list_views as $code){
				$list_fields[$code] = $i;
				$i += 100;
			}
			$form['list_view'] = $list_fields;
		}
		
		//检查属性组
		$attrs = $_POST['attrs'];
		if(!empty($attrs)){
			$attr_list = array();
			foreach($attrs as $c){
				$attr_list[$c] = 10000;
			}
			$form['attr_groups'] = $attr_list;
		}
		
		$form_mod = new FormsModel();
		if(empty($this->id)){
			//新增，检查名称是否已经存在
			if($form_mod->exists_attrs(array('site_id'=>SITE_ID, 'name'=>$name))){
				self::ajax_error('名称已经存在！');
			}
			$id = gen_guid();
			$form['_id'] = $id;
			$form_mod->insert($form);
			self::ajax_info('新增成功！', $id);
		}else{
			//修改，检查名称是否存在
			if($form_mod->exists_attrs(array('site_id'=>SITE_ID, 'name'=>$name, '_id'=>array('$ne'=>$this->id)))){
				self::ajax_error('名称已经存在！');
			}
			$form_mod->update_by_key_site_limit($this->id, $form);
			self::ajax_info('修改成功！', $this->id);
		}
	}
	
	/**
	 * 删除自定义表单
	 */
	public function del(){
		$form_mod = new FormsModel();
		if(empty($this->id)){
			self::ajax_error('未指定要删除的表单！');
		}
		//判断是否允许删除
		if($form_mod->exists_column_by_key($this->id, 'system')){
			self::ajax_error('系统表单，禁止删除！');
		}
		$form_mod->delete_by_key_site_limit($this->id);
		self::ajax_info('删除成功！');
	}
	
	public function group_edit(){
		if(IS_POST){
			self::group_save();
		}
		$gid = self::get_val('gid');
		$forms_mod = new FormsModel();
		if(empty($gid)){
			//新增
			$forms = $forms_mod->get_by_key_site_limit($this->id, 'mutil_lan');
			if(!isset($forms['mutil_lan']) || !$forms['mutil_lan']){
				$this->assign('site_langs', array(LANGUAGE=>Lang::get_lan_name(LANGUAGE)));
			}
		}else{
			//修改
			$forms = $forms_mod->get_by_key_site_limit($this->id, 'mutil_lan,groups.' . $gid);
			if(isset($forms['groups'][$gid])){
				$forms['groups'][$gid]['_id'] = $gid;
				$this->assign('group', $forms['groups'][$gid]);
			}
			if(!isset($forms['mutil_lan']) || !$forms['mutil_lan']){
				$this->assign('site_langs', array(LANGUAGE=>Lang::get_lan_name(LANGUAGE)));
			}
			$h = new AttrHelper(array('id'=>$this->id));
			$data = $h->format_forms_data();
			$data = lang_fatch($data, LANGUAGE);
			$ungroups = $data['ungroups'];
			$gs = $data['forms']['groups'];
			foreach ($gs as $g_id=>$g){
				if($g['_id'] === $gid && isset($g['items'])){
					foreach ($g['items'] as &$item){
						$item['selected'] = true;
					}
					$ungroups = array_merge($ungroups, $g['items']);
				}
			}
			$this->assign('ungroups', $ungroups);
		}
		$this->assign('id', $this->id);
		$this->display();
	}
	
	private function group_save(){
		$gid = self::get_post_val('gid');
		$group = array();
		$forms_mod = new FormsModel();
		$forms = $forms_mod->get_by_key_site_limit($this->id, 'mutil_lan');
		$langs = $this->site_info['langs'];
		if(!isset($forms['mutil_lan']) || !$forms['mutil_lan']){
			$langs = array(LANGUAGE => true);
		}
		foreach ($langs as $lang=>$is_default){
			$name = self::get_post_val($lang . '_name');
			$desc = self::get_post_val($lang . '_desc');
			$group[$lang] = array(
				'name' => $name,
				'desc' => $desc,
			);
		}
		$g_items = $_POST['items'];
		if(!empty($g_items)){
			foreach($g_items as $gi){
				if(empty($gi))continue;
				$group['items'][$gi] = 10000; 
			}
		}

		
		if(empty($gid)){
			//新增
			$forms_mod->update_by_key_site_limit($this->id, array('groups.' . gen_guid() => $group));
			self::ajax_info('新增成功！');
		}else{
			//修改
			$old_forms = $forms_mod->get_by_key_site_limit($this->id, 'groups.' . $gid . '.rank');
			$group['rank'] = $old_forms['groups'][$gid]['rank'];
			$forms_mod->update_by_key_site_limit($this->id, array('groups.' . $gid => $group));
			self::ajax_info('修改成功！');
		}
	}
	
	public function group_del(){
		$gid = self::get_val('gid');
		$forms_mod = new FormsModel();
		$forms_mod->delete_column_by_key($this->id, 'groups.' . $gid);
		self::ajax_info('分组删除成功！');
	}
	
	/**
	 * 排版
	 */
	public function typesetting(){
		if(IS_POST){
			//保存
			self::typesetting_save();
		}else{
			$h = new AttrHelper(array('id'=>$this->id));
			$forms_data = $h->format_forms_data();
			$this->assign('html', $h->get_html());
			$this->assign($forms_data);
			$this->display();
		}
	}
	
	private function typesetting_save(){
		if(empty($this->id)){
			self::ajax_error('未指定排序的表单！');
		}
		$codes = self::get_post_val('codes');
		if(empty($codes)){
			self::ajax_error('未指定序列！');
		}
		//保存
		$forms_mod = new FormsModel();
		$forms = $forms_mod->get_by_key_site_limit($this->id, 'groups');
		if(empty($forms)){
			self::ajax_error('未找到要排序的表单！');
		}
		$code_items = explode(',', $codes);
		$i = 1;
		foreach ($code_items as $item){
			if(empty($item))continue;
			if(!isset($forms['groups'][$item])){
				self::ajax_error('找不到分组，ID=' . $item);
			}
			$forms['groups'][$item]['rank'] = $i;
			++$i;
		}
		unset($forms['_id']);
		//保存
		$forms_mod->update_by_key_site_limit($this->id, $forms);
		self::ajax_info('排序保存成功！');
	}
	
	public function group_item_sort(){
		if(empty($this->id)){
			self::ajax_error('未指定排序的表单！');
		}
		$gid = self::get_post_val('gid');
		if(empty($gid)){
			self::ajax_error('未指定排序的表单Tab！');
		}
		$item_ids = self::get_post_val('item_ids');
		if(empty($item_ids)){
			self::ajax_error('未指定表单顺序！');
		}
		$forms_mod = new FormsModel();
		$forms = $forms_mod->get_by_key_site_limit($this->id, 'groups');
		if(empty($forms)){
			self::ajax_error('未找到要排序的表单！');
		}
		
		$item_names = explode(',', $item_ids);
		$i = 100;
		$items = array();
		foreach ($item_names as $item_name){
			if(empty($item_name))continue;
//			if(!isset($forms['groups'][$gid]['items'][$item_name])){
//				self::ajax_error('找不到表单条目，name=' . $item_name);
//			}
			$items[$item_name] = $i;
			$i += 100;
		}
		//保存
		$forms_mod->update_by_key_site_limit($this->id, array('groups.' . $gid . '.items'=>$items));
		self::ajax_info('排序保存成功！');
	}
	
	public function item_sort(){
		$ids_str = self::get_post_val('data');
		$entity_id = self::get_post_val('entity_id');
		$lang = self::get_post_val('lang');
		if(!empty($lang))$lang = '.' . $lang;
		$item_name = self::get_post_val('item_name');
		$module = self::get_post_val('module');
		$mod = m($module);
		$ids = split(';', $ids_str);
		$update_data = array();
		$i = 0;
		foreach ($ids as $id){
			if(empty($id))continue;
			$update_data[$item_name . $lang . '.' . $id . '.rank'] = $i;
			++$i;
		}
		$mod->update_by_key_site_limit($entity_id, $update_data);
		$this->ajax_info('排序保存成功！');
	}
	
	/**
	 * 抽取表单中的属性为组
	 */
	public function extract_attrs(){
		$codes = self::get_post_val('codes');
		if(empty($codes)){
			self::ajax_error('未指定要抽取的属性！');
		}
		$name = self::get_post_val('name');
		$desc = self::get_post_val('desc');
		if(empty($name) || utf8_strlen($name)>8){
			self::ajax_error('组名称不正确！');
		}
		if(empty($this->id)){
			self::ajax_error('未指定要抽取的表单！');
		}
		$code_list = explode(',', $codes);
		
		$form_mod = new FormsModel();
		$forms = $form_mod->get_by_key_site_limit($this->id, 'attr_groups,items');
		if(empty($forms)){
			self::ajax_error('未找到要抽取的表单！');
		}
		
		//抽取
		$items = array();
		foreach ($code_list as $k=>$item_code){
			if(empty($item_code))continue;
			if(isset($forms['items'][$item_code])){
				$items[$item_code] = $forms['items'][$item_code];
				unset($forms['items'][$item_code]);
			}
		}
		//创建组
		$attr_mod = new AttrsModel();
		$id = gen_guid();
		$attr_mod->insert(array(
			'_id' => $id,
			'name' => $name,
			'desc' => $desc,
			'items' => $items,
			'site_id' => SITE_ID,
		));

		//更新当前Forms
		$forms['attr_groups'][$id] = 10000;
		$form_mod->update_by_key_site_limit($this->id, $forms);
		self::ajax_info('抽取成功！');
	}
}

?>